package com.ruoyi.transport.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.utils.order.OrderUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.transport.domain.TTransportPlan;
import com.ruoyi.transport.service.ITTransportPlanService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 运输计划Controller
 * 
 * @author wzy
 * @date 2024-11-18 13:29:10
 */
@RestController
@RequestMapping("/transport/plan")
public class TTransportPlanController extends BaseController
{
    @Autowired
    private ITTransportPlanService tTransportPlanService;

    /**
     * 查询运输计划列表
     */
    @PreAuthorize("@ss.hasPermi('transport:plan:list')")
    @GetMapping("/list")
    public TableDataInfo list(TTransportPlan tTransportPlan)
    {
        startPage();
        List<TTransportPlan> list = tTransportPlanService.selectTTransportPlanList(tTransportPlan);
        return getDataTable(list);
    }

    /**
     * 导出运输计划列表
     */
    @PreAuthorize("@ss.hasPermi('transport:plan:export')")
    @Log(title = "运输计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TTransportPlan tTransportPlan)
    {
        List<TTransportPlan> list = tTransportPlanService.selectTTransportPlanList(tTransportPlan);
        ExcelUtil<TTransportPlan> util = new ExcelUtil<TTransportPlan>(TTransportPlan.class);
        util.exportExcel(response, list, "运输计划数据");
    }

    /**
     * 获取运输计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('transport:plan:query')")
    @GetMapping(value = "/{transportPlanId}")
    public AjaxResult getInfo(@PathVariable("transportPlanId") Long transportPlanId)
    {
        return success(tTransportPlanService.selectTTransportPlanByTransportPlanId(transportPlanId));
    }

    /**
     * 新增运输计划
     */
    @PreAuthorize("@ss.hasPermi('transport:plan:add')")
    @Log(title = "运输计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TTransportPlan tTransportPlan)
    {
        tTransportPlan.setTransportPlanNumber(OrderUtils.getOrderNumber("TP", "-"));
        tTransportPlan.setCreateBy(getNickName());
        return toAjax(tTransportPlanService.insertTTransportPlan(tTransportPlan));
    }

    /**
     * 修改运输计划
     */
    @PreAuthorize("@ss.hasPermi('transport:plan:edit')")
    @Log(title = "运输计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTransportPlan tTransportPlan)
    {
        Integer rtn = 0;
        tTransportPlan.setUpdateBy(getNickName());
        // 需要先更新，汽运计划，再计算其他字段
        rtn = tTransportPlanService.updateTTransportPlan(tTransportPlan);
        // 计算，已发车数，已发重量，已发件数，剩余重量，剩余件数
        if(rtn > 0) {
            tTransportPlanService.updateTTransportPlanDespatchWeightPieceByNumber(tTransportPlan.getTransportPlanNumber());
        }
        return toAjax(rtn);
    }

    /**
     * 删除运输计划
     */
    @PreAuthorize("@ss.hasPermi('transport:plan:remove')")
    @Log(title = "运输计划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{transportPlanIds}")
    public AjaxResult remove(@PathVariable Long[] transportPlanIds)
    {
        return toAjax(tTransportPlanService.deleteTTransportPlanByTransportPlanIds(transportPlanIds));
    }

    /**
     * 获取运输计划详细信息
     */
//    @PreAuthorize("@ss.hasPermi('transport:plan:query')")
    @GetMapping(value = "/ext/{transportPlanNumber}")
    public AjaxResult getInfoExt(@PathVariable("transportPlanNumber") String transportPlanNumber)
    {
        return success(tTransportPlanService.selectTTransportPlanByTransportPlanNumber(transportPlanNumber));
    }
}
